package com.cnpinxi.controller.admin.user;

import com.cnpinxi.controller.base.BaseController;
import com.cnpinxi.domain.AdminUser;
import com.cnpinxi.domain.Role;
import com.cnpinxi.myRunTime.MyRunTimeExcption;
import com.cnpinxi.result.ResultObj;
import com.cnpinxi.service.AdminUserService;
import com.cnpinxi.service.MenuService;
import com.cnpinxi.service.RoleService;
import com.cnpinxi.tool.RandomValidateCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * Created by Andrew  on 2017/2/26.
 */
@Controller
@RequestMapping(value = "/admin/role")
public class AdminRoleController extends BaseController {
@Autowired
    AdminUserService adminUserService;
  @Autowired
  MenuService menuService;
    @Autowired
    RoleService roleService;
    @RequestMapping(value="/getMenuListForRole",  method = RequestMethod.POST)
    @ResponseBody
    public String getMenuListForRole(HttpServletRequest request) {
        System.out.println("=================================getMenuListForRole==========================================>>>>>>");

        ResultObj resultObj = new ResultObj();
        resultObj.setCode(ResultObj.CODE_FAIL);
        try {
            Map<String, String> map = this.showRequestParameters(request);
            resultObj.setData(menuService.selectMenuListForRole(map));
            resultObj.setCode(ResultObj.CODE_SUCCESS);
            resultObj.setMsg("成功！");
        } catch (MyRunTimeExcption myRunTimeExcption) {

            resultObj.setMsg(myRunTimeExcption.getMessage());
            myRunTimeExcption.printStackTrace();
        } catch (Exception e) {
            resultObj.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return  resultObj.toJson();
    }

    @RequestMapping(value="/RoleAdd")
    @ResponseBody
    public ModelAndView RoleAdd(HttpServletRequest request) {
        ModelAndView m = new ModelAndView("/admin/role/RoleAdd");
        Map<String,String> map =this.showRequestParameters(request);
        if(map.get("tid")!=null){
            Role role = roleService.selectRole(map);
            m.addObject("role",role);
        }
        return  m;
    }

    @RequestMapping(value="/addRole",method = RequestMethod.POST)
    @ResponseBody
    public String addRole(HttpServletRequest request) {
        ResultObj resultObj = new ResultObj();
        resultObj.setCode(ResultObj.CODE_FAIL);
        try {
            Map<String, String> map = this.showRequestParameters(request);
            roleService.addRole(map);
            if(map.get("menuIds")!=null){
                roleService.modifyRole(map);
            }
            resultObj.setCode(ResultObj.CODE_SUCCESS);
            resultObj.setMsg("成功！");
        } catch (MyRunTimeExcption myRunTimeExcption) {
            resultObj.setMsg(myRunTimeExcption.getMessage());
            myRunTimeExcption.printStackTrace();
        } catch (Exception e) {
            resultObj.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return  resultObj.toJson();
    }

    @RequestMapping(value="/saveMenuRole",method = RequestMethod.POST)
    @ResponseBody
    public String saveMenuRole(HttpServletRequest request) {
        System.out.println("=================================getMenuListForRole==========================================>>>>>>");

        ResultObj resultObj = new ResultObj();
        resultObj.setCode(ResultObj.CODE_FAIL);
        try {
            Map<String, String> map = this.showRequestParameters(request);

            resultObj.setCode(ResultObj.CODE_SUCCESS);
            resultObj.setMsg("成功！");
            roleService.modifyRole(map);
        } catch (MyRunTimeExcption myRunTimeExcption) {

            resultObj.setMsg(myRunTimeExcption.getMessage());
            myRunTimeExcption.printStackTrace();
        } catch (Exception e) {
            resultObj.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return  resultObj.toJson();
    }
  @RequestMapping(value="/deleteMenu", method = RequestMethod.POST)
    @ResponseBody
    public String deleteMenu(HttpServletRequest request) {
        System.out.println("=================================getMenuListForRole==========================================>>>>>>");

        ResultObj resultObj = new ResultObj();
        resultObj.setCode(ResultObj.CODE_FAIL);
        try {
            Map<String, String> map = this.showRequestParameters(request);

            resultObj.setCode(ResultObj.CODE_SUCCESS);
            resultObj.setMsg("成功！");
            menuService.deleteMenu(map);
        } catch (MyRunTimeExcption myRunTimeExcption) {

            resultObj.setMsg(myRunTimeExcption.getMessage());
            myRunTimeExcption.printStackTrace();
        } catch (Exception e) {
            resultObj.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return  resultObj.toJson();
    }@RequestMapping(value="/addMenu", method = RequestMethod.POST)
    @ResponseBody
    public String addMenu(HttpServletRequest request) {
        System.out.println("=================================getMenuListForRole==========================================>>>>>>");

        ResultObj resultObj = new ResultObj();
        resultObj.setCode(ResultObj.CODE_FAIL);
        try {
            Map<String, String> map = this.showRequestParameters(request);

            resultObj.setCode(ResultObj.CODE_SUCCESS);
            resultObj.setMsg("成功！");
            menuService.addMenu(map);
        } catch (MyRunTimeExcption myRunTimeExcption) {

            resultObj.setMsg(myRunTimeExcption.getMessage());
            myRunTimeExcption.printStackTrace();
        } catch (Exception e) {
            resultObj.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return  resultObj.toJson();
    }


    @RequestMapping(value="/getRoleList")
    public ModelAndView getUserList(HttpServletRequest request) {
        System.out.println("=============================getRoleList=============================================>>>>>>");
        ModelAndView mov=new ModelAndView();
        mov.setViewName("/admin/role/getRoleList");

        Map<String,String> map=this.showRequestParameters(request);
        List<Role> roleList = roleService.getRoleList(map);
        mov.addObject("bean",map);
        mov.addObject("roleList",roleList);
        return mov;

    }


    @RequestMapping(value="/modifyUser", method = RequestMethod.POST)
    @ResponseBody
    public String modifyAdvert(HttpServletRequest request) {
        System.out.println("=================================modifyAdvert==========================================>>>>>>");

        ResultObj resultObj = new ResultObj();
        resultObj.setCode(ResultObj.CODE_FAIL);
        try {
            Map<String,String> map=this.showRequestParameters(request);

            adminUserService.modifyUser(map);
            resultObj.setCode(ResultObj.CODE_SUCCESS);
            resultObj.setMsg("成功！");
        }
        catch (MyRunTimeExcption myRunTimeExcption) {

            resultObj.setMsg(myRunTimeExcption.getMessage());
            myRunTimeExcption.printStackTrace();
        }catch (Exception e) {
            resultObj.setMsg(e.getMessage());
            e.printStackTrace();
        }

        return resultObj.toJson();

    }
    @RequestMapping(value="/getUser")
    public ModelAndView getAdvert(HttpServletRequest request) {
        System.out.println("=============================getAdvert==============================================>>>>>>");
        ModelAndView mov=new ModelAndView();
        mov.setViewName("/admin/user/getUser");
        AdminUser adminUser= null;
        try {
            adminUser = adminUserService.getUser(this.showRequestParameters(request));
            mov.addObject("adminUser",adminUser);
        } catch (MyRunTimeExcption e) {
            e.printStackTrace();
        }

        return mov;

    }
    @RequestMapping(value="/gotoLogin")
    public ModelAndView showAdvertList(HttpServletRequest request, Integer page, Integer pageSize) {
        System.out.println("=============================login=============================================>>>>>>");
        ModelAndView mov=new ModelAndView();
        mov.setViewName("/admin/user/login");

        return mov;

    }
    @RequestMapping(value = "/getVerificatioCode" ,produces = "application/json;charset=UTF-8",method = RequestMethod.GET)
    @ResponseBody
    public void getVerificationCode(HttpServletRequest request, HttpServletResponse response){
        response.setContentType("image/jpeg");// 设置相应类型,告诉浏览器输出的内容为图片
        response.setHeader("Pragma", "No-cache");// 设置响应头信息，告诉浏览器不要缓存此内容
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expire", 0);
        RandomValidateCode randomValidateCode = new RandomValidateCode();
        try {
            randomValidateCode.getRandomCode(request, response);// 输出图片方法
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @RequestMapping(value="/Login",produces = "application/json;charset=UTF-8" , method = RequestMethod.POST)
    @ResponseBody
    public String Login(HttpServletRequest request) {
        System.out.println("===========================================================================>>>>>>");

        ResultObj resultObj = new ResultObj();
        resultObj.setCode(ResultObj.CODE_SUCCESS);
        try {
Map<String,String> map=this.showRequestParameters(request);
          String   randomCode=(String)request.getSession().getAttribute(RandomValidateCode.RANDOMCODEKEY);
          String code=map.get("code");
          if(!code.equals(randomCode)){
              throw  new MyRunTimeExcption("验证码错误，登录失败！");
          }
            AdminUser adminUser=adminUserService.login(this.showRequestParameters(request));
            if(adminUser==null){
                throw  new MyRunTimeExcption("登录失败！");
            }
            request.getSession().setAttribute("user",adminUser);
            resultObj.setMsg("登录成功！");
        } catch (MyRunTimeExcption myRunTimeExcption) {
            myRunTimeExcption.printStackTrace();
            resultObj.setCode(ResultObj.CODE_FAIL);
            resultObj.setMsg(myRunTimeExcption.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            resultObj.setCode(ResultObj.CODE_FAIL);
            resultObj.setMsg(e.getMessage());
        }

        return resultObj.toJson();

    }
    @RequestMapping(value="/loginOut",produces = "application/json;charset=UTF-8" , method = RequestMethod.POST)
    @ResponseBody
    public String getAdvertList(HttpServletRequest request) {
        System.out.println("===============================loginOut============================================>>>>>>");

        ResultObj resultObj = new ResultObj();
        resultObj.setCode(ResultObj.CODE_SUCCESS);
        try {
            request.getSession().invalidate();
        } catch (MyRunTimeExcption myRunTimeExcption) {
            myRunTimeExcption.printStackTrace();
            resultObj.setCode(ResultObj.CODE_FAIL);
            resultObj.setMsg(myRunTimeExcption.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            resultObj.setCode(ResultObj.CODE_FAIL);
            resultObj.setMsg(e.getMessage());
        }

        return resultObj.toJson();

    }
}
